<?php

namespace app\admin\controller;

use app\admin\model\AlbumAudio as AlbumAudioModal;
use app\admin\validate\AlbumAudioValidate;
use app\BaseController;

class AlbumAudio extends BaseController
{
    protected $middleware = ['Auth'];

    public function list() {
        $params = $this->request->post(['current', 'size', 'superiorId', 'name']);
        $validate = new AlbumAudioValidate();
        if (!$validate->scene('list')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new AlbumAudioModal();
        $whereArr[] = ['superiorId', '=', $params['superiorId']];
        if(isset($params['name']) && $params['name']) {
            $whereArr[] = ["name", "LIKE", '%'.$params['name'].'%'];
        }
        $list = $sql
            ->where($whereArr)
            ->order('sort desc')
            ->order('create_at desc')
            ->paginate([
                'list_rows'=> request()->param('pageSize'),
                'page' => request()->param('pageNo'),
            ]);
        return result()->success($list);
    }

    public function add() {
        $params = $this->request->post(['superiorId','name', 'duration', 'url', 'sort']);
        $validate = new AlbumAudioValidate();
        if (!$validate->scene('add')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new AlbumAudioModal();
        $sql->save($params);
        return result()->success("添加成功");
    }

    public function addAll() {
        $params = $this->request->post(['list']);
        $sql = new AlbumAudioModal();
        $sql->saveAll($params['list']);
        return result()->success("添加成功");
    }

    public function edit() {
        $params = $this->request->post(['id', 'superiorId','name', 'duration', 'url', 'sort']);
        $validate = new AlbumAudioValidate();
        if (!$validate->scene('edit')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new AlbumAudioModal();
        $info = $sql->find($params['id']);
        $info->save($params);
        return result()->success("修改成功");
    }

    public function del() {
        $params = $this->request->post(['id']);
        $validate = new AlbumAudioValidate();
        if (!$validate->scene('del')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new AlbumAudioModal();
        $sql->destroy($params['id']);
        return result()->success("删除成功");
    }

    public function dels() {
        $params = $this->request->post(['ids']);
        $validate = new AlbumAudioValidate();
        if (!$validate->scene('dels')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new AlbumAudioModal();
        $sql->destroy($params['ids']);
        return result()->success("删除成功");
    }
}